/**
 * 
 */
package com.easyidea.its.facade.user.impl;

import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.easyidea.its.domain.model.system.Department;
import com.easyidea.its.domain.model.system.PassPort;
import com.easyidea.its.domain.model.system.Role;
import com.easyidea.its.domain.model.system.User;
import com.easyidea.its.domain.service.department.DepartmentService;
import com.easyidea.its.domain.service.department.PassPortService;
import com.easyidea.its.domain.service.system.RoleService;
import com.easyidea.its.domain.service.user.UserService;
import com.easyidea.its.facade.user.UserMgr;
import com.easyidea.its.web.dto.SeesionUserDto;
import com.easyidea.its.web.dto.UserDto;

/**
 * @author Administrator
 * 
 */
public class UserMgrImpl implements UserMgr {
	protected Log log = LogFactory.getLog(this.getClass());
	private UserService userService;
	private RoleService roleService;
	private PassPortService passPortService;
	private DepartmentService departmentService;
	
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	public void setPassPortService(PassPortService passPortService) {
		this.passPortService = passPortService;
	}
	public PassPortService getPassPortService() {
		return passPortService;
	}
	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public UserService getUserService() {
		return userService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.easyidea.its.facade.user.UserMgr#getCountOfUser(java.lang.String,
	 * java.lang.String)
	 */
	public int getCountOfUser(String userName, String name) {
		// TODO Auto-generated method stub
		return userService.getCountOfUser(userName, name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.easyidea.its.facade.user.UserMgr#getUsers(java.lang.String,
	 * java.lang.String, int, int)
	 */
	public Collection<User> getUsers(String name, String userName,
			int startAtIndex, int count) {
		// TODO Auto-generated method stub
		return userService.getUsers(name, userName, startAtIndex, count);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.easyidea.its.facade.user.UserMgr#hasUser(java.lang.Integer,
	 * java.lang.String)
	 */
	public boolean hasUser(Integer id, String userName) {
		// TODO Auto-generated method stub
		return userService.hasUser(id, userName);
	}
	public User getUserById(Integer id){
		return userService.getBeanById(id);
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.easyidea.its.facade.user.UserMgr#loginCheck(java.lang.String,
	 * java.lang.String)
	 */
	public SeesionUserDto loginCheck(String userName, String password) {
		User user = userService.loginCheck(userName, password);
		if (user == null)
			return null;
		SeesionUserDto userDto = getSessionUserDto(user);
		return userDto;
	} 
	public SeesionUserDto getUserPassPortsByUserId(Integer userId) {
		User user = this.userService.getUserPassPortsByUserId(userId);
		if (user == null)
			return null;
		SeesionUserDto userDto = getSessionUserDto(user);
		return userDto;		
	}
	private SeesionUserDto getSessionUserDto(User user) {
		SeesionUserDto userDto = new SeesionUserDto();
		userDto.setUserName(user.getUserName());
		userDto.setUserId(user.getId());
		userDto.setPermissionUrlList(user.getAllPermissionUrlList());
		userDto.setPermissionIdList(user.getAllPermissionIdList());
		userDto.setUserDisplayName(user.getName());
		if (user.getDepartment() != null) {
			userDto.setDeptId(user.getDepartment().getId());
			userDto.setDepartmentName(user.getDepartment().getDepartmentName());
			log.debug(user.getPassPortList());
			userDto.setPassPortList(user.getPassPortList());
		}
		return userDto;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public List<Role> getAllRoles() {
		return roleService.getAllRoles();
	}

	public void saveOrUpdateUser(User user) {
		saveUser(user);
	}

	/**
	 * @param user
	 */
	private void saveUser(User user) {
		if (user.getDepartment().getId() == -1) {
			user.setDepartment(null);
		}
		if (user.getId() == null || user.getId() == -1) {
			if (user.getRole() != null) {
				user.setRole(roleService.getBeanById(user.getRole().getId()));
			}
			
			userService.saveOrUpdateBean(user);
		} else {
			User oldUser = userService.getBeanById(user.getId());
			updateUserAndUserInfo(user, oldUser);
			userService.saveOrUpdateBean(oldUser);
		}
	}

	public int getUserCountByUserDto(UserDto userDto) {
		return userService.getUserCountByUserDto(userDto);
	}

	public Collection<User> getUsersByUserDto(UserDto userDto, int start,
			int pageSize) {
		return userService.getUsersByUserDto(userDto, start, pageSize);
	}

	public User getUserByUserId(Integer id) {
		return userService.getBeanById(id);
	}

	public void deleteUserById(Integer id) throws Exception {
		userService.removeBean(id);
	}
	public Collection<PassPort> getAllPassPortByDeptId(Integer deptId) {
		return passPortService.getAllPassPortByDeptId(deptId);
	}
	public void saveUserPassPort(Integer userId, Integer[] passPortId) {
		userService.saveUserPassPort(userId, passPortId);
	}
	/**
	 * @param user
	 * @param oldUser
	 */
	private void updateUserAndUserInfo(User user, User oldUser) {
		oldUser.setUserName(user.getUserName());
		oldUser.setName(user.getName());
		oldUser.setPassword(user.getPassword());
		oldUser.setStatus(user.getStatus());
		oldUser.setRole(user.getRole());
		oldUser.setDepartment(user.getDepartment());
	}
	public Collection<Department> getAllDepartments() {
		return departmentService.getAllDepartments();
	}
}
